﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _003_课程学习
{
    class Program
    {
        static void Main(string[] args)
        {
            /*for(int i=1; ;i++ )
            {
                Console.WriteLine("hhhh"+i);
                if (i > 10)
                {
                    break;
                }
            }*/

            //1到100的累加
            int sum = 0;
            for(int i = 1; i <= 100; i++)
            {
                sum += i;
            }
            Console.WriteLine(sum);
            //1到100的偶数累加
            sum = 0;
            for (int i = 2; i <= 100; i+=2)
            {
                sum += i;
            }
            Console.WriteLine(sum);
            //1到100的偶数累加
            sum = 0;
            for (int i = 1; i <= 100; i ++)
            {
                if (i % 2 == 1)
                {
                    continue;
                }

                sum += i;
            }
            Console.WriteLine(sum);


            //数组初始化
            //int[] arr = new int[5]; //分配了内存
            //int[] arr = new int[5]{1,2,3,4,5};//分配内存并且初始化
            int[] arr = {1,2,3,4,5};//分配内存并且初始化


            //二维数组与一维数组初始化差不多；
            int[,] arr2 = new int[2, 3] { 
                { 1,2,3},
                {4,5,6 }
                };
            for(int i = 0; i < 2; i++)
            {
                for(int j = 0; j < 3; j++)
                {
                    Console.Write(arr2[i,j]);
                }
                Console.WriteLine();
            }


            string[,] strArr =
            {
                {"#ff0000","黄" },
                {"#00ff00","绿"},
                {"#0000ff","蓝"},
                {"#000000","白" }
            };
            for (int i = 0; i < strArr.GetLength(0); i++)
            {
                for (int j =0; j < strArr.GetLength(1); j++)
                {
                    Console.Write(strArr[i, j] + " ");
                }
                Console.WriteLine();
            }


            //不规则数组
            int[][] arrInt = new int[4][];
            arrInt[0] =new int[4]{ 1,2,3,5};
            arrInt[1] = new int[5] { 8, 5, 2, 6, 4 };
            arrInt[2] = new int[1] {1};
            arrInt[3] = new int[3] { 2,7,8};

            foreach (int[] temp in arrInt)
            {
                foreach(var temp1 in temp)
                {
                    Console.Write(temp1 + " ");
                }
                Console.WriteLine();
            }

            char[][] charArr = new char[3][];
            charArr[0] = new char[5] { '我', '叫', 'x', 'x', 'x' };
            charArr[1] = new char[5] { '今', '年', 'x', 'x', '岁' };
            charArr[2] = new char[5] { '学', '号', 'x', 'x', 'x' };
            for (int i = 0; i < charArr.Length; i++)
            {
                for (int j = 0; j < charArr[i].Length; j++)
                {
                    Console.Write(charArr[i][j] + " ");
                }
                Console.WriteLine();
            }

            //system.Array方法
            int[] arrays = { 1, 5, 3, 8, 9, 3 };
            Array.Reverse(arrays);
            int[] arrs = arrays;
            for(int i = 0; i < arrs.Length; i++)
            {
                Console.Write(arrs[i]);
            }

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
            //冒泡排序
            int[] mao = { 1, 8, 6, 45, 39, 24, 52, 25, 4, 89 };
            int temp2 = 0;

            for(int i = 0; i < mao.Length; i++)
            {
                for(int j = 0; j < mao.Length - i - 1; j++)
                    {
                    if (mao[j] > mao[j + 1]){
                        temp2 = mao[j];
                        mao[j] = mao[j + 1];
                        mao[j + 1] = temp2;
                    }
                }
            }

            //错误思想，不可取
            /*for(int i = 0; i < mao.Length; i++)
            {
                for(int j = 1+i; j < mao.Length-i; j++)
                {
                    if (mao[i] > mao[j])
                    {
                        temp2 = mao[i];
                        mao[i] = mao[j];
                        mao[j] = temp2;
                    }
                }
            }*/

            for(int i = 0; i < mao.Length; i++)
            {
                Console.Write(mao[i]+" ");
            }

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();

            //快速排序
            int[] speed = { 1,25,58,32,16,29,94,62,300,5,54 };
            int left = 0;
            int right = speed.Length - 1;

            Sort(speed, left, right);
            Print(speed);

            Console.ReadKey();
        }
        public static void Sort(int[] arr, int left, int right)
        {
            if(left >= right)
            {
                return;
            }
            int index = SortUnit(arr, left, right);
            Sort(arr, left, index - 1);
            Sort(arr, index + 1, right);

        }

        public static void Print(int[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }
        }
        public static int SortUnit(int[] arr,int left,int right)
        {
            int key = arr[left];
            while (left < right)
            {
                while (left < right && arr[right] > key) { right--; }
                arr[left] = arr[right];
                while (left < right && arr[left] <= key) { left++; }
                arr[right] = arr[left];
            }
            arr[left] = key;
            return right;
        }
    }
}
